home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / SORTIO2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  3.5 KB  |  126 lines

  1. /*  sortio2.c - I/O finctions for MERGE2 program */
  2. /*  do_open , d0_close , getrec , putrec */
  3. #include   "stdio.h"
  4. #include   "cminor.h"
  5. #include   "sortspec.h"
  6.  
  7. extern    SORTSPEC sspec ;    /* has function address and */
  8.                 /* other sort spec. info */
  9. FILE *gfopen() ;
  10.  
  11. FILE *do_open(fname,fmode)    /* open file & check for errors */
  12.   char    fname[] ;        /* file name */
  13.   char    fmode[] ;        /* read/write/append mode */
  14.   {                /* return a file pointer */
  15.      FILE  *fd    ;
  16.  
  17.      fd = gfopen(fname,fmode,sspec.ftype) ;
  18.      if( fd == NULL )
  19.     {  printf("\n can't open file - %s \n",fname) ;
  20.        exit( 8 ) ;
  21.     }
  22.      return( fd ) ;
  23.   }
  24.  
  25.  
  26. int do_close(fd,fname)        /* close file & check for errors */
  27.   FILE    *fd ;            /* file pointer */
  28.   char    fname[] ;        /* name of file being closed */
  29.   {
  30.      if( fclose(fd) < 0 )
  31.     {  printf("\n can't close file - %s \n",fname) ;
  32.        exit( 10 ) ;
  33.     }
  34.   }
  35.  
  36.  
  37. int  getrec(rec,maxr,fd)    /* get a record */
  38.   char    rec[] ;         /* put it here in string form */
  39.   int    maxr ;            /* maximum length permitted */
  40.   FILE    *fd ;            /* file pointer for input file */
  41.   {                /* returns no. chars used in rec */
  42.      return( (*sspec.pget)(rec,maxr,fd)) ; /* use name get fun. */
  43.   }
  44.  
  45.  
  46. int  putrec(rec,fd)        /* output one record */
  47.   char    rec[] ;         /* put it here in string form */
  48.   FILE    *fd ;            /* output file pointer */
  49.   {
  50.      return( (*sspec.pput)(rec,fd) ) ; /* use specified put fun. */
  51.   }
  52.  
  53.  
  54. /* ******************************* line oriented functions **** */
  55.  
  56. int  getl(s,maxs,fd)        /* get one line from input file */
  57.   char    s[] ;            /* put it here in string form */
  58.   int    maxs ;            /* maximum length permitted */
  59.   FILE    *fd ;            /* file pointer for input file */
  60.   {                /* getl returns no. chars used in s */
  61.                 /* or -1 if EOF reached */
  62.      int   i ;
  63.                 /* get next line input */
  64.      if( fgets(s,maxs-1,fd) == NULL )
  65.     return( -1 ) ;        /* EOF - return special length value */
  66.      i = strlen(s) ;        /* get string length */
  67.      if( s[i-1] != '\n' )       /* see if a new-line is present */
  68.     {  s[i] = '\n' ;        /*  n - append one */
  69.        s[i+1] = '\0' ;      /* restore end-of-string marker */
  70.        i = i + 1 ;        /* sdjust string length */
  71.     }
  72.                 /* return length of line */
  73.      return(i+1) ;        /* count the '\0' at end too */
  74.   }
  75.  
  76.  
  77. int  putl(s,fd)         /* output one line of text */
  78.   char    s[] ;            /* line to output in string form */
  79.   FILE    *fd ;            /* output file pointer */
  80.   {
  81.      return( fputs(s,fd) ) ;    /* use fputs library function */
  82.   }
  83.  
  84.  
  85. /* ******************************* fixed length record functions*/
  86.  
  87. #define    BYTE_SIZE 1        /* fread/fwrite element size */
  88.  
  89. int  getr(rec,maxr,fd)        /* get a fixed length record */
  90.   char    rec[] ;         /* the record is here */
  91.   int    maxr ;            /* dummy size variable */
  92.   FILE    *fd ;            /* file stream pointer */
  93.   {
  94.      int   nr ;         /* number of bytes to read */
  95.  
  96.      nr = fread(rec,BYTE_SIZE,sspec.rec_size,fd) ;
  97.                 /* check for partial record */
  98.      if( (nr > 0 ) && (nr < sspec.rec_size) )
  99.     {  /* partial record */
  100.        printf("\n partial record at %1d \n",ftell(fd)) ;
  101.        exit( 2 ) ;
  102.     }
  103.      return( nr ) ;
  104.   }
  105.  
  106.  
  107. int  putr(rec,fd)        /* output a fixed length record */
  108.   char    rec[] ;         /* store the record here */
  109.   FILE    *fd ;            /* file streem pointer */
  110.   {
  111.      int   nw ;
  112.  
  113.      nw = fwrire(rec,BYTE_SIZE,sspec.rec_size,fd) ;
  114.                 /* check that entire record is written */
  115.      if( nw != sspec.rec_size )
  116.     { /* output problem */
  117.        printf("\n write error at %1d \n",ftell(fd)) ;
  118.        exit( 3 ) ;
  119.     }
  120.      return( nw ) ;
  121.   }
  122.  
  123.  
  124.  
  125.  
  126.